module.exports = {
    insert(collection, insertData) {
        return new Promise((resolve,reject)=> {
            collection.insertMany(insertData, (err) => {
                if(err) throw err;
                resolve()
            })
        })
    },
    delete(collection, deleteData, deleteNum) {
        return new Promise((resolve,reject) => {
            const deleteType = deleteNum === 1 ? 'deleteMany' : 'deleteOne';
            collection[deleteType](deleteData, (err) => {
                if(err) throw err;
                resolve()
            })
        })
    },
    update(collection,whereData,updateData,updateNum) {
        return new Promise((resolve,reject) => {
            const updateType = updateNum === 1 ? 'updateMany' : 'updateOne';
            collection[updateType](whereData,updateData, (err) => {
                if(err) throw err;
                resolve()
            })
        })
    },
    find(collection, whereData, showData) {
        return new Promise((resolve,reject) => {
            collection.find(whereData,showData,(err,data) => {
                if(err) throw err;
                resolve(data);
            })
        })
    },
    paging(collection, whereData, showData,limitnum, count) {
        return new Promise((resolve,reject) => {
            collection.find(whereData,showData).limit(limitnum).skip((count-1) * limitnum).exec((err,data) => {
                if(err) throw err;
                resolve(data);
            });
        })
    },
    sort(collection, whereData, showData, sortData) {
        return new Promise((resolve,reject) => {
            collection.find(whereData,showData).sort(sortData).exec((err,data) => {
                if(err) throw err;
                resolve(data);
            })
        })
    },
    distinct(collection, key) {
        return new Promise((resolve,reject) => {
            collection.distinct(key).exec((err,data) => {
                if(err) throw err;
                resolve(data);
            })
        })
    }

}